package com.huluobo.a2307douyindemo.adapter

import android.graphics.Color
import android.text.Spannable
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.huluobo.a2307douyindemo.R
import com.huluobo.lib_base.bean.MessageEntity

/**
 *  Created by LC on 2025/9/22.
 */
class MessageAdapter() : BaseQuickAdapter<MessageEntity, BaseViewHolder>(R.layout.item_chat_room) {
    override fun convert(holder: BaseViewHolder, item: MessageEntity) {
        val msg = "${item.username}:${item.content}"

        //富文本编辑器
        val builder = SpannableStringBuilder(msg)

        //定义几个富文本中的颜色
        val blue = ForegroundColorSpan(Color.BLUE)
        val red = ForegroundColorSpan(Color.RED)
        val yellow = ForegroundColorSpan(Color.YELLOW)

        if (item.username == "系统") {
            //富文本编辑器标记颜色,第一个参数是颜色值,第二个是标记起始下标,第三个是结束的下标,最后一个是标记的范围
            builder.setSpan(red, 0, item.username.length + 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
        } else {
            //除了系统其它都标记为蓝色
            builder.setSpan(blue, 0, item.username.length + 1, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
        }

        //所有内容标记为黄色
        builder.setSpan(yellow, item.username.length + 1, msg.length, Spannable.SPAN_INCLUSIVE_INCLUSIVE)
        holder.setText(R.id.item_chat_room_tv, builder)
    }
}